<?php

namespace app\common\model\area;

use think\Model;


class Area extends Model
{


    // 表名
    protected $name = 'area';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'level_text',
        'country_txt',
    ];


    public function getLevelList()
    {
        return ['1' => __('Level 1'), '2' => __('Level 2'), '3' => __('Level 3'), '0' => __('Level 0')];
    }


    public function getCountryList($pid = 0)
    {
        $country = $this->getModel()->where('pid', 0)->select();
        $data = array_merge(['0' => ['shortname' => '国家']], $country);
        return $data;
    }

    public function getProvinceList($pid = 0)
    {
        $data = $this->getModel()->where('pid', $pid)->select();
        return $data;
    }

    public function getCityList($pid = 0)
    {
        $country = $this->getModel()->where('pid', $pid)->select();
        return $country;
    }

    public function getCountryTxtAttr($value, $data)
    {
        return $this->getModel()->where('id', $data['pid'])->value('shortname');
    }

    public function getLevelTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['level']) ? $data['level'] : '');
        $list = $this->getLevelList();
        return isset($list[$value]) ? $list[$value] : '';
    }


}
